#region Summary

// 
// Owner : JackieHan 
// 
// Solution : UpdateSystem
// 
// Project :Project
// 
// File : FileUpDownloadImpl.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 实现<see href="IFileUpDownload" />
// 
// History : 
//			2011-01-06 17:33:33	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
using System.IO;
using Jackie.Update.IBusiness;

#endregion

namespace Jackie.Update.BusinessImpl
{
    /// <summary> 
    /// 实现<see href="IFileUpDownload" />
    /// </summary>
    public class FileUpDownloadImpl:IFileUpDownload
    {

        public byte[] Download(string filePath, int length, long offset)
        {
            if (!File.Exists(filePath))
                throw new FileNotFoundException("file not found", filePath);

            var buffer = new byte[length];
            using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                fileStream.Seek(offset, SeekOrigin.Begin);
                fileStream.Read(buffer, 0, length);
            }
            return buffer;
        }

        public void Upload(string filePath, byte[] buffer, long offset)
        {
            var file = new FileInfo(filePath);
            if (file.Directory != null && !file.Directory.Exists)
            {
                file.Directory.Create();
            }
            using (var filestream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))
            {
                filestream.Seek(offset, SeekOrigin.Begin);
                filestream.Write(buffer, 0, buffer.Length);
            }
        }
    }
}